package com.lottery.material.service.portal.user.impl;

import com.lottery.material.dao.article.ArticleActiveInfoMapper;
import com.lottery.material.dao.article.ArticleInfoMapper;
import com.lottery.material.dao.portaluser.CommentInfoMapper;
import com.lottery.material.pojo.portaluser.CommentInfo;
import com.lottery.material.service.portal.user.CommentInfoService;
import com.lottery.material.util.Const;
import com.lottery.material.vo.portal.comment.ArticleCommentVo;
import com.lottery.material.vo.portal.comment.UserComment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
@Service
@Slf4j
public class CommentInfoServiceImpl implements CommentInfoService {

    @Autowired
    CommentInfoMapper commentInfoMapper;
    @Autowired
    private ArticleInfoMapper articleInfoMapper;
    @Autowired
    private ArticleActiveInfoMapper articleActiveInfoMapper;

    /**
     * 根据用户查询用户列表
     * @Author 焦阳
     * @param paramMap
     * @return
     */
    @Override
    public List<UserComment> findCommentsByUserId(Map<String, Object> paramMap) {
        paramMap.put("indexStart", Const.PORTAL_PAGE*(Integer.parseInt(paramMap.get("page").toString())-1));
        paramMap.put("pageTotal",Const.PORTAL_PAGE);
        return commentInfoMapper.findCommentsByUserId(paramMap);
    }

    /**
     * description: 根据评论id查询
     * author sunny
     * createDate 2019-04-25 下午 3:08
     * @param commentId
     * @return
     */
    @Override
    public CommentInfo findByCommentId(String commentId) {
        return commentInfoMapper.findByCommentId(commentId);
    }

    /**
     * description: 分页查询文章评论列表
     * author sunny
     * createDate 2019-04-25 下午 5:02
     * @param paramMap
     * @return
     */
    @Override
    public List<ArticleCommentVo> findCommentPageList(Map<String, Object> paramMap) {
        return commentInfoMapper.findCommentPageList(paramMap);
    }

    /**
     * description: 保存文章评论信息
     * author sunny
     * createDate 2019-04-25 下午 8:25
     * @param commentInfo
     * @return
     */
    @Override
    public Integer saveCommentInfo(CommentInfo commentInfo) {
        Integer maxFloor = commentInfoMapper.findMaxFloorByArticleId(commentInfo.getArticleId());
        commentInfo.setFloor(maxFloor + 1);
        Integer count = commentInfoMapper.insertSelective(commentInfo);
        if(count > 0){
            articleInfoMapper.updateCommentTotalByArticleId(commentInfo.getArticleId(), "2");
            articleActiveInfoMapper.updateCommentTotalByArticleId(commentInfo.getArticleId(), "2");
            return 1;
        }else {
            return -1;
        }
    }

}
